package com.guodi.map.controller;


import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.guodi.common.utils.UuidUtil;
import com.guodi.core.boot.ctrl.BaseController;
import com.guodi.core.secure.utils.AuthUtil;
import com.guodi.core.tool.api.R;
import com.guodi.map.entity.MapSplitScreen;
import com.guodi.map.service.IMapSplitScreenService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import springfox.documentation.annotations.ApiIgnore;

import javax.annotation.Resource;
import javax.validation.constraints.NotBlank;
import java.util.Arrays;
import java.util.Date;
import java.util.List;

/**
* @描述: 分屏记录表 前端控制器
* @作者: 陈宏成
* @日期: 2021-11-10
*/
@Validated
@RestController
@RequestMapping("/MapSplitScreen")
@Api(value = "API - MapSplitScreenController", tags = "分屏记录表")
public class MapSplitScreenController extends BaseController {

    @Resource
    private IMapSplitScreenService iMapSplitScreenService;


    /**
    * @描述：新增 分屏记录表 记录
    * @作者: 陈宏成
    * @日期: 2021-11-10
    */
    @PostMapping("/save")
    @ApiOperation(value = "新增")
    @Transactional(rollbackFor = Exception.class)
    public R<MapSplitScreen> save(@Validated MapSplitScreen mapSplitScreen){
        // 新增前的验证数据，填充数据
        this.prepareBeforeSave(mapSplitScreen);
        // 新增
        boolean save = this.iMapSplitScreenService.save(mapSplitScreen);
        if (save){
            return R.data(mapSplitScreen);
        }else{
            throw new RuntimeException("新增失败");
        }
    }

    /**
    * @描述：修改 分屏记录表 记录
    * @作者: 陈宏成
    * @日期: 2021-11-10
    */
    @PostMapping("/update")
    @ApiOperation(value = "修改")
    @Transactional(rollbackFor = Exception.class)
    public R<MapSplitScreen> update(@Validated MapSplitScreen mapSplitScreen){
        // 修改前的验证数据，填充数据
        this.prepareBeforeUpdate(mapSplitScreen);
        // 修改
        MapSplitScreen oldEntity = this.iMapSplitScreenService.getById(mapSplitScreen.getId());
        LambdaUpdateWrapper<MapSplitScreen> updateWrapper = new LambdaUpdateWrapper<>();
        updateWrapper.setEntity(oldEntity);
        boolean update = this.iMapSplitScreenService.update(mapSplitScreen ,updateWrapper);
        if (update){
            return R.data(mapSplitScreen);
        }else{
            throw new RuntimeException("修改失败");
        }
    }


    /**
    * @描述：删除 分屏记录表 记录
    * @作者: 陈宏成
    * @日期: 2021-11-10
    */
    @PostMapping("/delete")
    @ApiOperation(value = "删除")
    @ApiImplicitParams({
        @ApiImplicitParam(name = "ids", value = "主键id，如果多个，英文逗号隔开", required = true, dataType = "string", paramType = "query")
    })
    @Transactional(rollbackFor = Exception.class)
    public R<List<MapSplitScreen>> delete(@NotBlank(message = "ids不能为空") String ids){
        List<String> idList = Arrays.asList(ids.trim().split(","));
        List<MapSplitScreen> mapSplitScreens = this.iMapSplitScreenService.listByIds(idList);
        boolean remove = this.iMapSplitScreenService.removeByIds(idList);
        if (remove){
            return R.data(mapSplitScreens);
        }else{
            throw new RuntimeException("删除失败");
        }
    }


    /**
    * @描述：获取 分屏记录表 列表
    * @作者: 陈宏成
    * @日期: 2021-11-10
    */
    @PostMapping("/page")
    @ApiOperation(value = "获取列表")
    @ApiImplicitParams({
        @ApiImplicitParam(name = "size", value = "页面大小，默认10", dataType = "int", paramType = "query"),
        @ApiImplicitParam(name = "current", value = "当前页码，默认1",  dataType = "int", paramType = "query"),
    })
    public R<Page<MapSplitScreen>> page(@ApiIgnore Page page){
        Page page1 = this.iMapSplitScreenService.pageUserSplitScreen(page, null);
        return R.data(page1);
    }



    /**
    * @描述：获取 分屏记录表 详情
    * @作者: 陈宏成
    * @日期: 2021-11-10
    */
    @PostMapping("/detail")
    @ApiOperation(value = "获取详情")
    @ApiImplicitParams({
        @ApiImplicitParam(name = "id", value = "主键id", required = true, dataType = "string", paramType = "query")
    })
    public R<MapSplitScreen> detail(@NotBlank(message = "id不能为空") String id){
        MapSplitScreen mapSplitScreen = this.iMapSplitScreenService.getById(id);
        return R.data(mapSplitScreen);
    }



    /**
    * @描述：修改前的准备，包括验证数据和填充数据
    * @入参：MapSplitScreen
    * @作者: 陈宏成
    * @日期: 2021-11-10
    */
    private void prepareBeforeUpdate(MapSplitScreen mapSplitScreen){
        if (!StringUtils.hasText(mapSplitScreen.getId())){
            throw new RuntimeException("请选择一个分屏记录进行修改");
        }
        // 判断是不是他自己的记录
        MapSplitScreen splitScreen = this.iMapSplitScreenService.getById(mapSplitScreen.getId());
        String userId = AuthUtil.getUserId().toString();
        if (!splitScreen.getCreator().equals(userId)){
            throw new RuntimeException("不能修改他人记录");
        }
        // 判断是否存在重名
        boolean checkNameExist = this.iMapSplitScreenService.checkNameExist(mapSplitScreen.getName(), AuthUtil.getUserId().toString(), null);
        if (checkNameExist){
            throw new RuntimeException("名称重复，请更换");
        }
        mapSplitScreen.setModifier(AuthUtil.getUserId().toString());
        mapSplitScreen.setModifyTime(new Date());
    }

    /**
    * @描述：新增前的准备，包括验证数据和填充数据
    * @入参：MapSplitScreen
    * @作者: 陈宏成
    * @日期: 2021-11-10
    */
    private void prepareBeforeSave(MapSplitScreen mapSplitScreen){
        // 判断是否存在重名
        boolean checkNameExist = this.iMapSplitScreenService.checkNameExist(mapSplitScreen.getName(), AuthUtil.getUserId().toString(), null);
        if (checkNameExist){
            throw new RuntimeException("名称重复，请更换");
        }
        mapSplitScreen.setId(UuidUtil.get32UUID());
        mapSplitScreen.setCreator(AuthUtil.getUserId().toString());
        mapSplitScreen.setCreateTime(new Date());
    }
}
